package com.mes.web.controller.hpf;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.mes.entity.hpf.SaleReconciliation;
import com.mes.entity.yzl.Condition;
import com.mes.entity.yzl.ListBack;
import com.mes.entity.yzl.Reconciliation;
import com.mes.entity.yzlExtend.ExtendProductor;
import com.mes.service.hpf.SaleReconciliationService;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

@Controller
@RequestMapping("/hpf")
public class SaleReconciliationController {

	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
	@Resource
	public SaleReconciliationService saleReconciliationService;
	
	/**
	 * 分页查询
	 * @param condition
	 * @return
	 */
    @RequestMapping("/reconciliationList")
    public @ResponseBody ListBack reconciliationList(Condition condition){
    	DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        ListBack listBack = new ListBack();
        List<SaleReconciliation> reconciliations = new ArrayList<>();
        List<Reconciliation> reconciliationList = new ArrayList<>();

        //获取当前月第一天
        Calendar fc = Calendar.getInstance();
        fc.add(Calendar.MONTH, 0);
        fc.set(Calendar.DAY_OF_MONTH,1);//设置为1号,当前日期既为本月第一天 
        String first = format.format(fc.getTime());
        //获取当前月最后一天
        Calendar lc = Calendar.getInstance();    
        lc.set(Calendar.DAY_OF_MONTH, lc.getActualMaximum(Calendar.DAY_OF_MONTH));  
        String last = format.format(lc.getTime());
        if(condition.getBeginDate() == null || condition.getBeginDate()==""){
        	condition.setBeginDate(first);
        	condition.setEndDate(last);
        	//查询条件处理
            reconciliations = saleReconciliationService.reconciliationPageList(condition);
            for (int i = 0; i < reconciliations.size(); i++) {
    			SaleReconciliation saleReconciliation = reconciliations.get(i);
    			Reconciliation r = new Reconciliation();
    	        r.setDateMonth(fc.get(Calendar.YEAR)+"-"+(fc.get(Calendar.MONTH)+1));
    	        r.setProductorId(saleReconciliation.getPid());
    	        Reconciliation r2 = saleReconciliationService.findByDateMonth(r);
    	        if(r2==null){
    	        	float price=Float.parseFloat(saleReconciliation.getRemark8());
    				int outCount = Integer.parseInt(saleReconciliation.getOutCount());
    				Reconciliation reconciliation = new Reconciliation();
    		        reconciliation.setDateMonth(fc.get(Calendar.YEAR)+"-"+fc.get(Calendar.MONTH));
    		        reconciliation.setProductorId(saleReconciliation.getPid());
    		        Reconciliation reconciliation2 = saleReconciliationService.findByDateMonth(reconciliation);
    		        Reconciliation reconciliation3 = new Reconciliation();
    		        if(reconciliation2!=null){
    		        	reconciliation3.setLastMonthQ(reconciliation2.getClosingBalanceQ());
    		        	reconciliation3.setLastMonthP(reconciliation2.getClosingBalanceQ()*price);
    		        	reconciliation3.setClosingBalanceQ(reconciliation2.getClosingBalanceQ()+outCount);
    		        	reconciliation3.setClosingBalanceP((reconciliation2.getClosingBalanceQ()+outCount)*price);
    		        }else{
    		        	reconciliation3.setLastMonthQ(0);
    		        	reconciliation3.setLastMonthP(0);
    		        	reconciliation3.setClosingBalanceQ(outCount);
    		        	reconciliation3.setClosingBalanceP(outCount*price);
    		        }
    		        reconciliation3.setMonthQ(outCount);
    		        reconciliation3.setMonthP(outCount*price);
    		        reconciliation3.setProductorId(saleReconciliation.getPid());
    		        reconciliation3.setDateMonth(fc.get(Calendar.YEAR)+"-"+(fc.get(Calendar.MONTH)+1));
    		        int isSuccess = saleReconciliationService.addReconciliation(reconciliation3);
    	        }else{
    	        	float price=Float.parseFloat(saleReconciliation.getRemark8());
    	        	int outCount = 0;
    	        	if(saleReconciliation.getOutCount()!=null&&!"".equals(saleReconciliation.getOutCount())){
    	        		outCount = Integer.parseInt(saleReconciliation.getOutCount());
    	        	}
    				r2.setMonthQ(outCount);
    				r2.setMonthP(outCount*price);
    				r2.setClosingBalanceQ(r2.getLastMonthQ()+outCount-r2.getMonthSettlementQ());
    				r2.setClosingBalanceP(r2.getLastMonthP()+(outCount*price)-r2.getMonthSettlementP());
    		        int isSuccess = saleReconciliationService.resetReconciliation(r2);
    	        }
    		}
        }else{
            int year = Integer.parseInt(condition.getBeginDate().split("-")[0]);
            int month = Integer.parseInt(condition.getBeginDate().split("-")[1]);
            Calendar zc = Calendar.getInstance();
            zc.set(Calendar.YEAR, year);
            zc.set(Calendar.MONTH, month - 1);
            zc.set(Calendar.DATE, 1);
            zc.roll(Calendar.DATE, -1);
            int maxDate = zc.get(Calendar.DATE);
            condition.setBeginDate(year+"-"+month+"-1");
        	condition.setEndDate(year+"-"+month+"-"+maxDate);
        	//查询条件处理
            reconciliations = saleReconciliationService.reconciliationPageList(condition);
            for (int i = 0; i < reconciliations.size(); i++) {
    			SaleReconciliation saleReconciliation = reconciliations.get(i);
    			Reconciliation r = new Reconciliation();
    	        r.setDateMonth(year+"-"+month);
    	        r.setProductorId(saleReconciliation.getPid());
    	        Reconciliation r2 = saleReconciliationService.findByDateMonth(r);
    	        if(r2==null){
    	        	float price=Float.parseFloat(saleReconciliation.getRemark8());
    	        	int outCount = 0;
    	        	if(saleReconciliation.getOutCount()!=null&&!"".equals(saleReconciliation.getOutCount())){
    	        		outCount = Integer.parseInt(saleReconciliation.getOutCount());
    	        	}
    				Reconciliation reconciliation = new Reconciliation();
    		        reconciliation.setDateMonth(year+"-"+(month-1));
    		        reconciliation.setProductorId(saleReconciliation.getPid());
    		        Reconciliation reconciliation2 = saleReconciliationService.findByDateMonth(reconciliation);
    		        Reconciliation reconciliation3 = new Reconciliation();
    		        if(reconciliation2!=null){
    		        	reconciliation3.setLastMonthQ(reconciliation2.getClosingBalanceQ());
    		        	reconciliation3.setLastMonthP(reconciliation2.getClosingBalanceQ()*price);
    		        	reconciliation3.setClosingBalanceQ(reconciliation2.getClosingBalanceQ()+outCount);
    		        	reconciliation3.setClosingBalanceP((reconciliation2.getClosingBalanceQ()+outCount)*price);
    		        }else{
    		        	reconciliation3.setLastMonthQ(0);
    		        	reconciliation3.setLastMonthP(0);
    		        	reconciliation3.setClosingBalanceQ(outCount);
    		        	reconciliation3.setClosingBalanceP(outCount*price);
    		        }
    		        reconciliation3.setMonthQ(outCount);
    		        reconciliation3.setMonthP(outCount*price);
    		        reconciliation3.setProductorId(saleReconciliation.getPid());
    		        reconciliation3.setDateMonth(year+"-"+month);
    		        int isSuccess = saleReconciliationService.addReconciliation(reconciliation3);
    	        }else{
    	        	float price=Float.parseFloat(saleReconciliation.getRemark8());
    	        	int outCount = 0;
    	        	if(saleReconciliation.getOutCount()!=null&&!"".equals(saleReconciliation.getOutCount())){
    	        		outCount = Integer.parseInt(saleReconciliation.getOutCount());
    	        	}
    				r2.setMonthQ(outCount);
    				r2.setMonthP(outCount*price);
    				r2.setClosingBalanceQ(r2.getLastMonthQ()+outCount-r2.getMonthSettlementQ());
    				r2.setClosingBalanceP(r2.getLastMonthP()+(outCount*price)-r2.getMonthSettlementP());
    		        int isSuccess = saleReconciliationService.resetReconciliation(r2);
    	        }
    		}
        }
        condition.setBeginDate(condition.getBeginDate().split("-")[0]+"-"+Integer.parseInt(condition.getBeginDate().split("-")[1]));
        int count = saleReconciliationService.reconciliationAllCount(condition);
        listBack.setTotal(count);
        reconciliationList = saleReconciliationService.reconciliationAllPageList(condition);
        listBack.setRows(reconciliationList);
        return listBack;
    }
    
    /**
     * 对账详情
     * @param id
     * @return
     */
    @RequestMapping("/reconciliationXQ")
    public @ResponseBody List<ExtendProductor> reconciliationXQ(Condition condition){
    	DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
    	List<ExtendProductor> extendProductors = new ArrayList<>();

        //获取当前月第一天
        Calendar fc = Calendar.getInstance();    
        fc.add(Calendar.MONTH, 0);
        fc.set(Calendar.DAY_OF_MONTH,1);//设置为1号,当前日期既为本月第一天 
        String first = format.format(fc.getTime());
        //获取当前月最后一天
        Calendar lc = Calendar.getInstance();    
        lc.set(Calendar.DAY_OF_MONTH, lc.getActualMaximum(Calendar.DAY_OF_MONTH));  
        String last = format.format(lc.getTime());
        if(condition.getBeginDate() == null || condition.getBeginDate()==""){
        	condition.setBeginDate(first);
        	condition.setEndDate(last);
        }else{
        	String beginDate = condition.getBeginDate();
        	int year = Integer.parseInt(condition.getBeginDate().split("-")[0]);
            int month = Integer.parseInt(condition.getBeginDate().split("-")[1]);
            Calendar zc = Calendar.getInstance();
            zc.set(Calendar.YEAR, year);
            zc.set(Calendar.MONTH, month - 1);
            zc.set(Calendar.DATE, 1);
            zc.roll(Calendar.DATE, -1);
            int maxDate = zc.get(Calendar.DATE);
            condition.setBeginDate(beginDate+"-01");
            condition.setBeginDate(beginDate+"-"+maxDate);
        }
        //查询条件处理
        extendProductors = saleReconciliationService.reconciliationXQ(condition);
        return extendProductors;
    }
    
    //添加对账信息
    @RequestMapping("/addReconciliationFrom")
    public @ResponseBody int addReconciliationFrom(@RequestBody Object obj) throws ParseException {
    	int addIsSuccess = 0;
        JSONObject jsonObject = JSONObject.fromObject(obj);

        JSONArray jsonArray = jsonObject.getJSONArray("productors");

        try {
            addIsSuccess = saleReconciliationService.updateReconciliation(jsonArray);
        }catch (Exception e){
            addIsSuccess = 0;
        }
        return addIsSuccess;
    }
	
}
